using Unity.Mathematics;
using EarthUtility;
using Geometry;
using System;

namespace EarthGeospatial
{

    public interface IProjection
    {
        GlobeRectangle unproject(Rectangle rectangle);
    }
    public class Projection
    {
        enum ProjectionType
        {
            Geographic = 0,
            WebMercator = 1
        }
        private ProjectionType curUseProjection;
        private GeographicProjection geographicProjection;
        private WebMercatorProjection webMercatorProjection;
        public Projection()
        {

        }

        public Projection(WebMercatorProjection webMercatorProjection)
        {
            useProjection(webMercatorProjection);
        }
        public Projection(GeographicProjection geo)
        {
            useProjection(geo);
        }

        public void useProjection(GeographicProjection geo)
        {
            geographicProjection = geo;
            curUseProjection = ProjectionType.Geographic;
        }
        public void useProjection(WebMercatorProjection web)
        {
            webMercatorProjection = web;
            curUseProjection = ProjectionType.WebMercator;
        }

        public static GlobeRectangle unprojectRectangleSimple(Projection projection, Rectangle rectangle)
        {
            switch (projection.curUseProjection)
            {
                case ProjectionType.Geographic:
                    return projection.geographicProjection.unproject(rectangle);
                case ProjectionType.WebMercator:
                    return projection.webMercatorProjection.unproject(rectangle);
                default:
                    UnityEngine.Debug.LogError("不支持的投影类型");
                    return null;
            }
        }

        internal static Rectangle projectRectangleSimple(Projection projection, GlobeRectangle rectangle)
        {
            switch (projection.curUseProjection)
            {
                case ProjectionType.Geographic:
                    return projection.geographicProjection.project(rectangle);
                case ProjectionType.WebMercator:
                    return projection.webMercatorProjection.project(rectangle);
                default:
                    UnityEngine.Debug.LogError("不支持的投影类型");
                    return null;
            }
        }

        internal static double3 projectPosition(Projection projection, Cartographic position)
        {
            switch (projection.curUseProjection)
            {
                case ProjectionType.Geographic:
                    return projection.geographicProjection.project(position);
                case ProjectionType.WebMercator:
                    return projection.webMercatorProjection.project(position);
                default:
                    UnityEngine.Debug.LogError("不支持的投影类型");
                    return double3.zero;
            }
        }


        public static double computeApproximateConversionFactorToMetersNearPosition(Projection projection, double2 position)
        {
            switch (projection.curUseProjection)
            {
                case ProjectionType.Geographic:
                    return 1.0;
                case ProjectionType.WebMercator:
                    return math. cos(projection.webMercatorProjection.unproject(position).latitude);
                default:
                    UnityEngine.Debug.LogError("不支持的投影类型");
                    return 0.0;
            }
        }
    }
}